package com.example.pongv1;
import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import android.graphics.Color;

import com.example.pongv1.Constantes.LEVEL;
import com.example.pongv1.MainActivity;
public class RessourcesManager {
	
	 //---------------------------------------------
    // VARIABLES
    //---------------------------------------------
    
    private static final RessourcesManager INSTANCE = new RessourcesManager();
    
    public Engine engine;
    public MainActivity activity;
    public Camera camera;
    public VertexBufferObjectManager vbom;
    public Font font;
    public Font font2;
    public Font font3;
    public Font font4;
    
    
    //---------------------------------------------
    // TEXTURES & TEXTURE REGIONS
    //---------------------------------------------
    //splash
    public ITextureRegion splash_region;
    private BitmapTextureAtlas splashTextureAtlas;
    
    //menu
    public ITextureRegion menu_background_region;
    public ITextureRegion play_region;
    public ITextureRegion options_region;
    public ITextureRegion multi_region;
    public ITextureRegion info_region;
    private BuildableBitmapTextureAtlas menuTextureAtlas;
    //pongScene
    private BuildableBitmapTextureAtlas Pong_SceneTextureAtlas;
    public ITextureRegion scene_background_region;
    public ITextureRegion platform1_region;
    public ITextureRegion ball_region;
    //options scene
    public ITextureRegion option_background_region;
    public ITextureRegion soundon_region;
    public ITextureRegion soundoff_region;
    public ITextureRegion level1_region;
    public ITextureRegion level2_region;
    public ITextureRegion level3_region;
    public ITextureRegion level4_region;
    public ITextureRegion level5_region;
    public ITextureRegion level6_region;
    public ITextureRegion level7_region;
    public ITextureRegion level8_region;
    public ITextureRegion level9_region;
    public ITextureRegion level10_region;
    public ITextureRegion selectlevel_region;
    public ITextureRegion name_region;
  
    private BuildableBitmapTextureAtlas optionTextureAtlas;
    //infoscene
    public ITextureRegion info_background_region;
    public ITextureRegion reset_region;
    private BuildableBitmapTextureAtlas infoTextureAtlas;
    public String nialevel="None";
	public String nrank="Rookie";
	public String nSerialswins="0";
	public String ntotwin="0";
	public String ntotdefeat="0";
	public String ntoprank="0";
    //sons
    public Music music;
    public boolean sound_activated=true;
    public Sound butSound;
    public Sound collisionSound;
    public Sound game_overSound;
    public Sound racketSound;
    //IA
    
    public LEVEL IAlevel=LEVEL.IALEVEL1;
    //
    public String playername="player 1";
    //Data base
    public  final String PREFS_NAME = "MyPrefsFile";

    //---------------------------------------------
    // CLASS LOGIC
    //---------------------------------------------

    public void loadMenuResources()
    {
        loadMenuGraphics();
        loadMenuAudio();
        loadMenuFonts();
    }
    
    public void loadGameResources()
    {
        loadGameGraphics();
        loadGameFonts();
        loadGameAudio();
        
        
    }
    public void loadOptionResources()
    {
        loadOptionGraphics();
      
        
    }
    public void loadInfoResources()
    {
    	nialevel="None";
		nrank="Rookie";
		nSerialswins="0";
		ntotwin="4";
		ntotdefeat="0";
		ntoprank="Rookie";
        loadInfoGraphics();
      
        
    }
    
    private void loadOptionGraphics() {
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    	optionTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
    	option_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "texture2.png");
    	soundon_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "SOUNDSON.png");
    	soundoff_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "SOUNDSOFF.png");
    	level1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "1.png");
    	level2_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "2.png");
    	level3_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "3.png");
    	level4_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "4.png");
    	level5_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "5.png");
    	level6_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "6.png");
    	level7_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "7.png");
    	level8_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "8.png");
    	level9_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "9.png");
    	level10_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "10.png");
    	reset_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "reset.png");
    	selectlevel_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "selectlevel.png");
    	name_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, activity, "name.png");

    	try 
    	{
    	    this.optionTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
    	    this.optionTextureAtlas.load();
    	} 
    	catch (final TextureAtlasBuilderException e)
    	{
    	        Debug.e(e);
    	} 
		
	}
    private void loadInfoGraphics() {
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    	infoTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
    	info_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(infoTextureAtlas, activity, "texture2.png");
    	
    	reset_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(infoTextureAtlas, activity, "reset.png");

    	try 
    	{
    	    this.infoTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
    	    this.infoTextureAtlas.load();
    	} 
    	catch (final TextureAtlasBuilderException e)
    	{
    	        Debug.e(e);
    	} 
		
	}

	private void loadMenuGraphics()
    {
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    	menuTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
    	menu_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "texture2.png");
    	play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "solo.png");
    	options_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "options.png");
    	multi_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "multi.png");
    	info_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "infos.png");    
    	try 
    	{
    	    this.menuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
    	    this.menuTextureAtlas.load();
    	} 
    	catch (final TextureAtlasBuilderException e)
    	{
    	        Debug.e(e);
    	} 
    }
    
    public void loadMenuAudio()
    {
    	 MusicFactory.setAssetBasePath("sounds/");
  		try {
  			music = MusicFactory.createMusicFromAsset(activity.getMusicManager(),activity, "Sandstorm.ogg");
  			music.setLooping(true);
  			
  		} catch (final IOException e) {
  			Debug.e(e);
  		}
    }


    private void loadGameGraphics()
    {
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    	Pong_SceneTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
    	scene_background_region=BitmapTextureAtlasTextureRegionFactory.createFromAsset(Pong_SceneTextureAtlas, activity, "texture1.png");
    	
    	platform1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Pong_SceneTextureAtlas, activity, "platform1.png");
    	ball_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Pong_SceneTextureAtlas, activity, "ball.png");
    	
    	       
    	try 
    	{
    	    this.Pong_SceneTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
    	    this.Pong_SceneTextureAtlas.load();
    	} 
    	catch (final TextureAtlasBuilderException e)
    	{
    	        Debug.e(e);
    	} 
        
    }
    
    private void loadGameFonts()
    {
        
    }
    
    private void loadGameAudio()
    {
    	
    	 SoundFactory.setAssetBasePath("sounds/");
    	 try {
  			this.butSound = SoundFactory.createSoundFromAsset(activity.getSoundManager(),activity, "but.ogg");
  			this.game_overSound = SoundFactory.createSoundFromAsset(activity.getSoundManager(),activity, "game_over.ogg");
  			this.collisionSound = SoundFactory.createSoundFromAsset(activity.getSoundManager(),activity, "murscol.ogg");
  			this.racketSound = SoundFactory.createSoundFromAsset(activity.getSoundManager(),activity, "swoosh.ogg");
    	 } catch (final IOException e) {
  			Debug.e(e);
  		}
    	
    	
    }
    
    public void loadSplashScreen()
    {
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    	splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
    	splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.png", 0, 0);
    	splashTextureAtlas.load();
    
    }
    
    public void unloadSplashScreen()
    {
    	splashTextureAtlas.unload();
    	splash_region = null;
    }
    
    /**
     * @param engine
     * @param activity
     * @param camera
     * @param vbom
     * <br><br>
     * We use this method at beginning of game loading, to prepare Resources Manager properly,
     * setting all needed parameters, so we can latter access them from different classes (eg. scenes)
     */
    public static void prepareManager(Engine engine, MainActivity activity, Camera camera, VertexBufferObjectManager vbom)
    {
        getInstance().engine = engine;
        getInstance().activity = activity;
        getInstance().camera = camera;
        getInstance().vbom = vbom;
    }
    
    //---------------------------------------------
    // GETTERS AND SETTERS
    //---------------------------------------------
    
    public static RessourcesManager getInstance()
    {
        return INSTANCE;
    }
    
    private void loadMenuFonts()
    {
        FontFactory.setAssetBasePath("font/");
        final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        final ITexture subFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        final ITexture subFont2Texture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        final ITexture subFont3Texture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

        font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50f, true, Color.WHITE, 2f, Color.BLACK);
        font.load();
        font2 = FontFactory.createStrokeFromAsset(activity.getFontManager(), subFontTexture, activity.getAssets(), "BELL.ttf", 40f, true, Color.rgb(237, 127, 16), 2f,Color.BLACK );
        font2.load();
        font3 = FontFactory.createStrokeFromAsset(activity.getFontManager(), subFont2Texture, activity.getAssets(), "BELL.ttf", 70f, true, Color.rgb(237, 127, 16), 2f,Color.BLACK );
        font3.load();
        font4 = FontFactory.createStrokeFromAsset(activity.getFontManager(), subFont3Texture, activity.getAssets(), "BELL.ttf", 50f, true,Color.WHITE, 2f, Color.WHITE );
        font4.load();
    }
    
    public void unloadMenuTextures()
    {
        menuTextureAtlas.unload();
    }
        
    public void loadMenuTextures()
    {
        menuTextureAtlas.load();
    }
    public void unloadPongSceneTextures()
    {
        Pong_SceneTextureAtlas.unload();
    }
        
    public void loadPongSceneTextures()
    {
        Pong_SceneTextureAtlas.load();
    }
    
    
    
    public void unloadOptionTextures()
    {
        optionTextureAtlas.unload();
    }
        
    public void loadOptionTextures()
    {
        optionTextureAtlas.load();
    }
    public void unloadInfoTextures()
    {
        infoTextureAtlas.unload();
    }
        
    public void loadInfoTextures()
    {
        infoTextureAtlas.load();
    }

}
